package test07;

import java.util.Arrays;

public class StudentSort {
    public static void main(String[] args) {
        Student students[]=new Student[5];
        students[0]=new Student(3,"wang",20,"man");
        students[1]=new Student(2,"liu",43,"woman");
        students[2]=new Student(1,"hu",18,"man");
        students[3]=new Student(6,"zhang",22,"woman");
        students[4]=new Student(7,"chen",30,"man");
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }
}
class Student implements Comparable{
    int no;
    String name;
    int age;
    String sex;

    public Student(int no, String name, int age, String sex) {
        this.no = no;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Student student=(Student)o;
        if(this.no!=student.no){    //学号不相同时
            return this.no-student.no;
        }else{      //学号相同时
            if(this.name.charAt(0)!=student.name.charAt(0)){    //名字首字母不相同时
                return this.name.charAt(0)-student.name.charAt(0);
            }else{  //名字首字母相同时
                if(this.age!=student.age){  //年龄不相同时
                    return this.age-student.age;
                }else{  //年龄相同时
                    return this.sex.length()-student.sex.length();
                }
            }
        }
    }
}
